import math
import threading
import tkinter as tk
import tkinter.messagebox as tm

import search

wi = tk.Tk()
a = tk.StringVar(value='0')
b = tk.StringVar(value='0')
mo = tk.IntVar()

res = 0
m = 0
n = 0


def run():
    aa = a.get()
    bb = b.get()
    if aa == '' or bb == '':
        tm.showerror("错误", "请填满每个框")
        return
    aa = float(aa)
    bb = float(bb)
    mm = int(mo.get())
    global res
    if mm == 0:
        res = aa + bb
    elif mm == 1:
        res = aa - bb
    elif mm == 2:
        res = aa * bb
    elif mm == 3:
        if bb == 0:
            tm.showerror("错误", "b不能为0")
            return
        res = aa / bb
    elif mm == 4:
        if bb < 0:
            tm.showerror("错误", "b必须>=0")
            return
        res = aa * math.sqrt(bb)
    else:
        if aa == 0 and bb == 0:
            tm.showerror("错误", "a、b不能同时为0")
            return
        res = aa ** bb

    tk.Label(wi, text="结果：").place(x=1, y=210)
    tk.Button(wi, text='ans', command=ans).place(x=123, y=175)
    tk.Label(wi, text=' ' * 99).place(x=1, y=232)  # 清除原有的字
    tk.Label(wi, text=str(res)).place(x=1, y=232)
    tk.Label(wi, text="正在转换...").place(x=1, y=254)
    th = threading.Thread(target=zh)
    th.start()


def zh():
    global res
    ress = search.search(res)
    tk.Label(wi, text=' ' * 99).place(x=1, y=254)  # 清除原有的字
    tk.Label(wi, text=str(ress)).place(x=1, y=254)


def ans():
    global a, res
    a = tk.StringVar(value=str(res))
    tk.Entry(wi, textvariable=a).place(x=77, y=1)


def mp():
    global m
    m += res


def mm():
    global m
    m -= res


def mc():
    global m
    m = 0


def mr():
    global a
    a = tk.StringVar(value=m)
    tk.Entry(wi, textvariable=a).place(x=77, y=1)


def np():
    global n
    n += res


def nm():
    global n
    n -= res


def nc():
    global n
    n = 0


def nr():
    global a
    a = tk.StringVar(value=n)
    tk.Entry(wi, textvariable=a).place(x=77, y=1)


def atb():
    global b
    b = tk.StringVar(value=a.get())
    tk.Entry(wi, textvariable=b).place(x=77, y=33)


def init():
    global wi
    wi.geometry('333x333')

    tk.Label(wi, text="第一个数：").place(x=1, y=1)
    tk.Entry(wi, textvariable=a).place(x=77, y=1)

    tk.Label(wi, text="第二个数：").place(x=1, y=33)
    tk.Entry(wi, textvariable=b).place(x=77, y=33)

    tk.Label(wi, text="运算符：").place(x=1, y=66)
    tk.Radiobutton(wi, text="﹢", variable=mo, value=0).place(x=55, y=66)
    tk.Radiobutton(wi, text="﹣", variable=mo, value=1).place(x=55, y=88)
    tk.Radiobutton(wi, text="×", variable=mo, value=2).place(x=55, y=110)
    tk.Radiobutton(wi, text="÷", variable=mo, value=3).place(x=55, y=132)
    tk.Radiobutton(wi, text="√", variable=mo, value=4).place(x=55, y=154)
    tk.Radiobutton(wi, text="^", variable=mo, value=5).place(x=110, y=66)

    tk.Button(wi, text=' ' * 9 + "计算" + ' ' * 9, command=run).place(x=1, y=175)

    tk.Button(wi, text='M+', command=mp).place(x=165, y=66)
    tk.Button(wi, text='M-', command=mm).place(x=165, y=99)
    tk.Button(wi, text='MC', command=mc).place(x=165, y=132)
    tk.Button(wi, text='MR', command=mr).place(x=165, y=165)
    tk.Button(wi, text='N+', command=np).place(x=220, y=66)
    tk.Button(wi, text='N-', command=nm).place(x=220, y=99)
    tk.Button(wi, text='NC', command=nc).place(x=220, y=132)
    tk.Button(wi, text='NR', command=nr).place(x=220, y=165)
    tk.Button(wi, text='a to b', command=atb).place(x=234, y=1)


if __name__ == '__main__':
    init()
    wi.mainloop()
